import api from './request'

// 消息类型
export interface UserMessage {
  id: number
  userId: number
  postId?: number
  commentId?: number
  resourceId?: number
  messageType: string // 'comment' | 'like' | 'system'
  content: string
  isRead: number // 0-未读，1-已读
  time: string
  postName?: string // 帖子标题
  resourceName?: string // 资源名称
}

// 分页响应类型
export interface MessagePageResponse {
  content: UserMessage[]
  totalPages: number
  totalElements: number
  size: number
  number: number
  first: boolean
  last: boolean
  empty: boolean
}

// 获取用户消息列表（支持按已读状态和消息类型筛选）
export const getUserMessages = (
  userId: number,
  messageType: number, // 0-系统消息，1-点赞和评论消息
  page: number = 0,
  size: number = 10,
  type: number = -1 // 0-未读，1-已读，-1-全部
): Promise<MessagePageResponse> => {
  return api.get(`/messages/user/${userId}`, {
    params: {
      page,
      size,
      type,
      messageType
    }
  })
}

// 更新消息状态为已读
export const updateMessageStatus = (messageId: number) => {
  return api.post('/messages/updateStatus', null, {
    params: {
      Id: messageId
    }
  })
}

// 全部消息设为已读
export const markAllMessagesAsRead = (userId: number) => {
  return api.post('/messages/markAllRead', null, {
    params: { userId }
  })
}

// 判断用户是否有未读消息（支持消息类型参数）
export const hasUnreadMessages = (
  userId: number,
  messageType: number = -1
): Promise<{ code: number; message: string; data: boolean }> => {
  return api.get(`/messages/user/${userId}/hasUnread`, {
    params: { messageType }
  })
}

// 统计三种类型（postLike/comment/system/resourceLike）未读消息数量
export const getUnreadMessageCountByType = (
  userId: number
): Promise<{ code: number; message: string; data: { postLike: number; comment: number; system: number; resourceLike: number } }> => {
  return api.get('/messages/user/unreadCount', {
    params: { userId }
  })
}
